package com.truongnha.clienttn.business.service;

import android.content.Context;

import com.truongnha.clienttn.business.model.SavedAccountModel;
import com.truongnha.clienttn.data.dao.CreateDatabase;
import com.truongnha.clienttn.data.dao.SavedAccountDAO;
import com.truongnha.clienttn.data.entity.SavedAccountEntity;

public class SavedAccountService {
	Context context;

	public SavedAccountService(Context context) {
		super();
		this.context = context;
	}

	public void save(String username, String password) {
		CreateDatabase db = new CreateDatabase(context);
		SavedAccountDAO savedAccountDAO = new SavedAccountDAO(db.open());
		savedAccountDAO.delete();
		savedAccountDAO.insert(username, password);
		db.close();

	}

	public SavedAccountModel get() {
		CreateDatabase db = new CreateDatabase(context);
		SavedAccountModel savedAccountModel;
		SavedAccountDAO saveAccountDAO = new SavedAccountDAO(db.open());

		SavedAccountEntity savedAccountEntity = saveAccountDAO.getAll();
		if (savedAccountEntity == null)
			return null;
		else
			savedAccountModel = new SavedAccountModel(
					savedAccountEntity.getUsername(),
					savedAccountEntity.getPassword());
		db.close();
		return savedAccountModel;
	}
}
